<template>
  <el-dialog
    title="大华视频"
    :visible.sync="dialogVisible"
    width="70%"
    :modal="false"
    :before-close="handleClose"
  >
    <el-form :inline="true" label-width="80px">
      <el-form-item>
        <el-date-picker
          v-model="dateTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00','23:59:59']"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          :picker-options="pickerOptions"
          type="datetimerange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table class="mt10" v-loading="loading" :data="dataList" border stripe>
      <el-table-column label="文件名称" prop="fileName" align="center"/>
      <el-table-column label="开始时间" align="center" min-width="30">
        <template slot-scope="scope">
          {{ moment(scope.row.beginTime).format('yyyy-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" min-width="30">
        <template slot-scope="scope">
          {{ moment(scope.row.endTime).format('yyyy-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="20">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handlePreviews(scope.row)">预览</el-button>
          <el-button size="mini" type="text" icon="el-icon-link" @click="handleBind(scope.row)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { getDhVideoRecord } from '@/api/dh'
import moment from 'moment'
import { insertTaskVideoRecord } from '@/api/oms/task'

export default {
  name: 'DhVideoRecord',
  props: {
    taskId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      dateTime: [],
      record: undefined,
      dialogVisible: false,
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setHours(0, 0, 0, 0)
              end.setHours(23, 59, 59, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '昨天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setHours(0, 0, 0, 0)
              end.setHours(23, 59, 59, 0)
              start.setDate(start.getDate() - 1)
              end.setDate(end.getDate() - 1)
              picker.$emit('pick', [start, end])
            }
          }]
      },
      total: 0,
      loading: false
    }
  },
  computed: {},
  methods: {
    moment,
    handleClose() {
      this.dialogVisible = false
    },
    handlePreviews(row) {
      window.open(row.filePath, '_blank')
    },
    handleBind(row) {
      this.$modal.confirm('是否绑定视频记录？').then(() => {
        let data = {
          taskId: this.taskId,
          fileName: row.fileName,
          url: row.filePath,
          endTime: row.endTime,
          beginTime: row.startTime
        }
        this.$modal.loading('视频绑定中....')
        insertTaskVideoRecord(data).then(() => {
          this.$modal.msgSuccess('绑定成功')
          this.dialogVisible = false
          this.$emit('refresh')
        }).finally(() => {
          this.$modal.closeLoading()
        })
      })

    },
    resetQuery() {
      this.daterangeCreateTime = []
      this.getList()
    },
    handleQuery() {
      this.getList()
    },
    getList() {
      this.loading = true
      if (this.dateTime.length) {
        let params = {
          ...this.queryParams,
          beginDate: this.dateTime[0],
          endDate: this.dateTime[1]
        }
        getDhVideoRecord(params).then(res => {
          if (res.data.results) {
            this.dataList = res.data.results
            this.total = res.data.totalCount
          } else {
            this.$message.error(res.data.message)
          }
        }).finally(() => {
          this.loading = false
        })
      } else {
        return this.$message.error('请选择时间')
      }
    }
  }
}
</script>

<style scoped>

</style>
